package ui.servlet;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import po.Account;
import po.Cart;
import po.Dish;
public class IndexServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		HttpSession session = request.getSession();
		Account account = (Account) session.getAttribute("account");
		ServletContext servletContext = request.getServletContext();
		Cart cart = null;
		Dish dish = null;
		List<Cart> cartList = new ArrayList<Cart>();
		Map<Integer,Integer> carts = (Map<Integer,Integer>)session.getAttribute("carts");
		if(carts != null && account != null){
			Map<Integer,Dish> dishMap = (Map<Integer,Dish>)request.getServletContext().getAttribute("dishMap");
			servletContext.setAttribute(account.getAid(), carts);
			for(Integer i : carts.keySet()){
				cart = new Cart();
				dish = dishMap.get(i);
				cart.setAid(account.getAid());
				cart.setDid(i);
				cart.setCount(carts.get(i));
				cart.setDish(dish);
				cartList.add(cart);
			}
		}
		request.setAttribute("cartList", cartList);
		
		request.getRequestDispatcher("index.jsp").forward(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}
